'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class MorphMetadata

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal directiveStatus As IStatusDirective)
         Dim statusoutput As IStatusOutput
         Dim morphMetadata As CodeGenerationSupport.IMorphMetadataDirective
         Dim outputFile As String
         Dim xmlDocIn As New Xml.XmlDocument
         Dim metadataRoot As KADGen.MorphMetadata.ORMMappingRoot
         Dim xmlDocOut As Xml.XmlDocument
         morphMetadata = CType(node, CodeGenerationSupport.IMorphMetadataDirective)
         outputFile = morphMetadata.SinglePass.OutputFile
         statusoutput = directiveStatus.AddStatusOutput( _
                  outputFile, _
                  FileTreatment.Unknown)
         Try
            xmlDocIn.Load(morphMetadata.InputFileName)
            metadataRoot = KADGen.MorphMetadata.ORMMappingRoot.LoadXML(xmlDocIn, _
                  morphMetadata.ObjectsFromTables, _
                  morphMetadata.ObjectsFromViews)
            'TODO: Add Preprocess
            xmlDocOut = metadataRoot.Morph( _
                  morphMetadata.IncludeBuildForSP, _
                  morphMetadata.IncludeAssembly, _
                  morphMetadata.IncludeDataStructure, _
                  morphMetadata.CompatibilityMode)
            'Diagnostics.Debug.WriteLine(metadataRoot.Report)
            'FUTURE: Add Preprocess
            'FUTURE: Use normal mechanisms for output - SInglePass
            xmlDocOut.Save(outputFile)
            directiveStatus.CountCreated = 1
            directiveStatus.CountSelected = 1
            directiveStatus.CountOutput = 1
         Catch ex As System.Exception
            statusoutput.AddStatusException("MorphMetadata.Run", "Failure creating file", ex)
         End Try
         statusoutput.Status = statusoutput.ChildStatus
      End Sub
#End Region
   End Class
End Namespace